package com.example.androidlearning2400.ui.act

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.KeyboardArrowRight
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.Checkbox
import androidx.compose.material3.CheckboxDefaults
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.OutlinedTextFieldDefaults
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.text.input.PasswordVisualTransformation
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.example.androidlearning2400.R
import com.example.androidlearning2400.ui.act.ui.theme.AndroidLearning2400Theme

class LoginActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            // 应用Material3主题
            MaterialTheme {
                LoginScreen() // 显示登录界面
            }
        }
    }
}

// 登录界面组件
@Composable
fun LoginScreen() {
    // 输入框状态
    var phone by remember { mutableStateOf("") }
    var password by remember { mutableStateOf("") }
    var code by remember { mutableStateOf("") }
    var isRemember by remember { mutableStateOf(false) }

    // 页面主体布局
    Column(
        modifier = Modifier
            .fillMaxSize()
            .background(MaterialTheme.colorScheme.background)
            .padding(24.dp),
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        // 1. 应用图标（画板1）
        Image(
            painter = painterResource(id = R.drawable.huaban1), // 确保此图标存在
            contentDescription = "应用图标",
            modifier = Modifier
                .size(100.dp)
                .padding(vertical = 32.dp)
        )

        // 2. 标题
        Text(
            text = "登录",
            fontSize = 24.sp,
            color = MaterialTheme.colorScheme.onBackground,
            modifier = Modifier.padding(bottom = 24.dp)
        )

        // 3. 手机号输入框（手机图标）
        OutlinedTextField(
            value = phone,
            onValueChange = { phone = it },
            label = { Text("手机号") },
            leadingIcon = {
                Image(
                    painter = painterResource(id = R.drawable.shouji), // 确保此图标存在
                    contentDescription = "手机图标",
                    modifier = Modifier.size(24.dp)
                )
            },
            keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Phone),
            singleLine = true,
            modifier = Modifier.fillMaxWidth(),
            shape = RoundedCornerShape(8.dp)
        )

        Spacer(modifier = Modifier.height(16.dp))

        // 4. 验证码输入框（画板5图标）+ 获取按钮
        Row(
            modifier = Modifier.fillMaxWidth(),
            horizontalArrangement = Arrangement.spacedBy(12.dp)
        ) {
            OutlinedTextField(
                value = code,
                onValueChange = { code = it },
                label = { Text("验证码") },
                leadingIcon = {
                    Image(
                        painter = painterResource(id = R.drawable.huaban5), // 确保此图标存在
                        contentDescription = "验证码图标",
                        modifier = Modifier.size(24.dp)
                    )
                },
                keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number),
                singleLine = true,
                modifier = Modifier.weight(1f),
                shape = RoundedCornerShape(8.dp)
            )

            Button(
                onClick = { /* 获取验证码逻辑 */ },
                modifier = Modifier
                    .width(100.dp)
                    .height(56.dp)
            ) {
                Text("获取")
            }
        }

        Spacer(modifier = Modifier.height(16.dp))

        // 5. 密码输入框（密码图标）
        OutlinedTextField(
            value = password,
            onValueChange = { password = it },
            label = { Text("密码") },
            leadingIcon = {
                Image(
                    painter = painterResource(id = R.drawable.mima), // 确保此图标存在
                    contentDescription = "密码图标",
                    modifier = Modifier.size(24.dp)
                )
            },
            visualTransformation = PasswordVisualTransformation(),
            singleLine = true,
            modifier = Modifier.fillMaxWidth(),
            shape = RoundedCornerShape(8.dp)
        )

        // 6. 记住密码
        Row(
            modifier = Modifier.fillMaxWidth(),
            verticalAlignment = Alignment.CenterVertically,
            horizontalArrangement = Arrangement.Start
        ) {
            Checkbox(
                checked = isRemember,
                onCheckedChange = { isRemember = it }
            )
            Text("记住密码", fontSize = 14.sp)
        }

        // 7. 登录按钮
        Button(
            onClick = { /* 登录逻辑 */ },
            modifier = Modifier
                .fillMaxWidth()
                .height(56.dp)
                .padding(top = 24.dp)
        ) {
            Row(
                verticalAlignment = Alignment.CenterVertically,
                modifier = Modifier.fillMaxWidth(),
                horizontalArrangement = Arrangement.Center
            ) {
                Text("登录", fontSize = 18.sp)
                Spacer(modifier = Modifier.width(8.dp))
                Image(
                    imageVector = Icons.Filled.KeyboardArrowRight,
                    contentDescription = "箭头",
                    modifier = Modifier.size(20.dp)
                )
            }
        }

        // 8. 第三方登录（画板6图标）
        Column(
            modifier = Modifier.padding(top = 40.dp),
            horizontalAlignment = Alignment.CenterHorizontally
        ) {
            Text("其他登录方式", fontSize = 14.sp)
            Spacer(modifier = Modifier.height(16.dp))
            Image(
                painter = painterResource(id = R.drawable.huaban6), // 确保此图标存在
                contentDescription = "第三方登录",
                modifier = Modifier.size(40.dp)
            )
        }
    }
}

// 预览功能
@Preview(showBackground = true)
@Composable
fun LoginScreenPreview() {
    MaterialTheme {
        LoginScreen()
    }
}